;
(function(){
	
	var cirBtn,
		leOrNeBtn,
		imgs ,
		trigger,
		INTERVAL = 5000,
		STEP = 960,
		interTime,
		i,
		u,
		isToLeft,
		isRight,
		isLeft,
		stepX,
		lx,
		isOver;
		
	var imgPlay = function(){
		
		cirBtn = $('.J_cirBtn'),
		leOrNeBtn = $('.J_leOrNeBtn'),
		imgs = $('.J_focBanner li'),
		trigger = $('.J_focBanner');
		
		_autoPlay();
		_bindBtn();	
	};
	
	function _autoPlay(){
			
			i = 0,
			isToLeft = true;
			setTimeFun(i);
	};
	
	function setTimeFun(i){
		u = cirBtn.length;
		
		clearInterval(interTime);
		interTime = setInterval(function(){
			isOver = false;
			if(isToLeft){
				
				i += 1;	
				if(i === u-1){
					isToLeft = false;	
				}	
			}else{
				
				i -= 1;
				if(i === 0){
					isToLeft = true;	
				}	
			}
			
			_isLeftOrRight(i);
			_changeImg(i);	
			_changeBtnSta(i);
			
		},INTERVAL);		
	};
	
	function _isLeftOrRight(i){
		if(i === u-1){
			isRight = true;
		}	else{
			isRight = false;	
		}
		
		if(i === 0){
			isLeft = true;
		}	else{
			isLeft = false;	
		}
	};
	
	function _changeImg(i){
		
		$(trigger).animate({
			'margin-left' : -STEP * i + 'px'		
		},INTERVAL/7,function(){
			isOver = true;	
		});
	};
	
	function _changeBtnSta(i){
		
		cirBtn.each(function(j){
			$(cirBtn[j]).removeClass('on');	
		});
		$(cirBtn[i]).addClass('on');
		
		leOrNeBtn.each(function(j){
			$(leOrNeBtn[j]).removeClass('disable');	
		});
		
		if(isRight){
			$('.J_neBtn').addClass('disable');	
		}
		if(isLeft){
			$('.J_leBtn').addClass('disable');		
		}
	};
	
	function _bindBtn(){
		cirBtn.each(function(j){
			$(cirBtn[j]).mouseover(function(){
					clearInterval(interTime);
					$(trigger).stop();
					_isLeftOrRight(j);
					_changeImg(j);
					_changeBtnSta(j);	
			});
			
			$(cirBtn[j]).mouseout(function(){
				setTimeFun(j);
			});
		});	
		
		leOrNeBtn.each(function(k){
			$(leOrNeBtn[k]).click(function(){
				
				if(isOver){
					isOver = false;
					clearInterval(interTime);
					
					if(k===0 && i!==0){
						i = i - 1; 	
					}else if(k===leOrNeBtn.length-1 && i!==imgs.length-1){
						i = i + 1;	
					}else{
						i = i;	
					}	
					
					if(k===0 && i===0){
						isToLeft = true;	 	
					}else if(k===leOrNeBtn.length-1 && i===imgs.length-1){
						isToLeft = false;	
					}
	
					_isLeftOrRight(i);
					_changeImg(i);
					_changeBtnSta(i);	
					setTimeFun(i);		
				}
			});	
		});
	};
	
	$(document).ready(function(){
		imgPlay();	
	});	
})();